﻿/*var CountryManager = function($http, $q, Country) {
    var CountryManager = {
        _pool: {},
        _retrieveInstance: function(CountryData) {
            var instance = this._pool[CountryData];

            if (instance) {
                instance.setData(CountryData);
            } else {
                instance = new Country(CountryData);
                this._pool[CountryId] = instance;
            }

            return instance;
        },
        _search: function(CountryId) {
            return this._pool[CountryId];
        },
        _load: function(CountryId, deferred) {
            var scope = this;

            $http.get('ourserver/Country/' + CountryId)
                .success(function(CountryData) {
                    var Country = scope._retrieveInstance(CountryData.id, CountryData);
                    deferred.resolve(Country);
                })
                .error(function() {
                    deferred.reject();
                });
        },
        /* Public Methods #1#
        /* Use this function in order to get a Country instance by it's id #1#
        getCountry: function(CountryId) {
            var deferred = $q.defer();
            var Country = this._search(CountryId);
            if (Country) {
                deferred.resolve(Country);
            } else {
                this._load(CountryId, deferred);
            }
            return deferred.promise;
        },
        /* Use this function in order to get instances of all the Country #1#
        loadAllCountry: function() {
            var deferred = $q.defer();
            var scope = this;
            $http.get('ourserver/Country)
                .success(function(CountryArray) {
                    var Country = [];
                    CountryArray.forEach(function(CountryData) {
                        var Country = scope._retrieveInstance(CountryData.id, CountryData);
                        Country.push(Country);
                    });

                    deferred.resolve(Country);
                })
                .error(function() {
                    deferred.reject();
                });
            return deferred.promise;
        },
        /*  This function is useful when we got somehow the Country data and we wish to store it or update the pool and get a Country instance in return #1#
        setCountry: function(CountryData) {
            var scope = this;
            var Country = this._search(CountryData.id);
            if (Country) {
                Country.setData(CountryData);
            } else {
                Country = scope._retrieveInstance(CountryData);
            }
            return Country;
        },

    };
    return CountryManager;
}*/